<template>
  <div class="">
    <el-form :inline="true">
      <div style="display: flex; justify-content: space-between;">
        <div>
          <el-form-item>
            <el-input v-model="key" clearable placeholder="姓名、手机号、昵称" style="width: 300px;" />
          </el-form-item>
          <el-form-item>
            <label>状态：</label>
            <el-select v-model="state" placeholder="状态" @change="init">
              <el-option label="全部" :value="-1" />
              <el-option label="已处理" :value="1" />
              <el-option label="未处理" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="init">查询</el-button>
          </el-form-item>
        </div>
        <div />
      </div>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="userid" label="用户ID" width="90" align="center" />
      <el-table-column prop="truname" label="姓名" align="center" />
      <el-table-column prop="username" label="手机号" align="center" />
      <el-table-column prop="contentstr" label="反馈内容" align="center" />
      <el-table-column prop="content" label="反馈图片" align="center">
        <template slot-scope="{row}">
          <el-image style="width: 50px; height: 50px" :src="row.imgList[0]" :preview-src-list="row.imgList" />
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="125" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0">未处理</el-tag>
          <el-tag v-if="scope.row.state == 1" type="success">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="replycontent" label="回复消息" width="200" align="center" />
      <el-table-column prop="ctime" label="反馈时间" width="200" align="center" />
      <el-table-column label="操作" width="160px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="onVisible(1, scope.row)">处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="block " style="padding-top: 20px;">
      <el-pagination background :current-page="PageIndex" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="totalCount" @size-change="idx => { pageSize = idx }"
        @current-change="idx => { PageIndex = idx }" />
    </div>

    <el-dialog :title="visibleType == '1' ? '意见处理' : '修改分类'" :visible.sync="dialogVisible" width="500px"
      :close-on-click-modal="false" @closed="handleClose">
      <div>
        <el-form ref="forms" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="回复内容" prop="content">
            <el-input v-model="form.content" type="textarea" placeholder="请输入回复内容" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Feedback_GetMsg, Feedback_Reply, Sys_SendMsg } from '@/api/msg.js'
export default {
  name: 'Feedback',
  data() {
    return {
      // 筛选参数
      key: '',
      state: -1,

      // 列表相关
      listLoading: false,
      tableData: [],
      totalCount: 0,
      PageIndex: 1,
      pageSize: 10,


      // 操作对话框相关
      dialogVisible: false,
      visibleType: '',
      form: {},
      rules: {
        content: [
          { required: true, message: '请输入回复内容', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    PageIndex(val) {
      console.log(val)
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    /**刷新加载 */
    async init() {
      this.PageIndex = 1
      this.getData()
    },
    /**对话框打开前（处理操作） */
    onVisible(type, item = {}) {
      this.visibleType = type
      this.form = {
        id: item.id,
        content: ''
      }
      this.dialogVisible = true
    },
    /** 对话框关闭前 */
    handleClose() {
      this.$refs.forms.resetFields()
    },
    /**主列表 */
    async getData() {
      this.listLoading = true
      const { list, pageIndex, totalCount } = await Feedback_GetMsg({
        key: this.key,
        state: this.state,
        PageIndex: this.PageIndex,
        PageSize: this.pageSize
      })
      this.totalCount = totalCount
      this.tableData = []
      console.log(list)
      list.forEach(element => {
        element.ctime = element.ctime.replace('T', ' ')
        const fkInfo = JSON.parse(element.content)
        element.contentstr = fkInfo.content;
        element.imgList = fkInfo.imgList;
        this.tableData.push(element)
      })
      this.listLoading = false
    },
    /**操作提交 */
    onEdit() {
      this.$refs['forms'].validate(async (valid) => {
        if (valid) {
          const { success, msg } = await Feedback_Reply(this.form)
          if (success) {
            this.dialogVisible = false
            this.getData()
          }
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        } else {
          return false
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped></style>
<style lang="scss">
.el-table .warning-row {
  background: oldlace;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 100%;
  display: block;
}
</style>
